<?php

error_reporting(E_ALL);

class FormException extends Exception {
    public function errorMessage() {
        $errorMsg = 'Wrong argument in form: ' . $this->getMessage();
        return $errorMsg;
    }
}

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require "../vendor/autoload.php";

function check_arguments($arr) {
    foreach ($arr as $key) {
        if (! array_key_exists($key, $_POST)) {
            throw new FormException("Missing argument: " . $key);
        }
    }
}

sleep(10);

?><!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">

        <title>Setup session</title>

        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="robots" content="noindex, nofollow">

        <link rel="stylesheet" href="/assets/css/simple.min.css" integrity="sha384-RU21UwWLAoiCR1pUjxpW4YprCd2Pdvbi1kFN8QPxkI3gXdF5GrL+OH5GVg/Mvwa5">
    </head>
    <body>
        <header>
            <h1>Setup session</h1>
        </header>
        <main>
<?php

try {

    $passwd = rtrim(file_get_contents("../passwd.txt"), "\n");

    $required_arguments = [
        "request",
        "entity",
        "tunnel_type",
        "ipv6-ll",
        "email",
        "website",
        "location",
        "comments",
        "captcha"
    ];
    check_arguments($required_arguments);

    if ($_POST["request"] != "true") {
        throw new FormException("The form was sent incorrectly.");
    }

    if ($_POST["captcha"] != "42") {
        throw new FormException("Captcha incorrect!");
    }

    $entity = $_POST["entity"];
    $email = $_POST["email"];
    $tunneltype = $_POST["tunnel_type"];

    $message = "Hello,\n\nI would like to peer with you. The following are my details.\n\n";

    $message .= "Entity: entitydb/" . $entity . ".json\n";
    $message .= "E-Mail: " . $email . "\n";
    $message .= "Website: " . $_POST["website"] . "\n";
    $message .= "\n";

    $message .= "Tunnel type: ";

    switch ($tunneltype) {
        case "wireguard":
            $message .= "WireGuard";
            break;
        case "fastd":
            $message .= "fastd";
            break;
        case "openvpn":
            $message .= "OpenVPN";
            break;
        case "gre":
            $message .= "GRE";
            break;
        case "vxlan":
            $message .= "VXLAN";
            break;
        default:
            throw new FormException("Wrong tunnel type");
            break;
    }
    $message .= "\n";

    $message .= "Location: " . $_POST["location"] . "\n";
    $message .= "IPv6 link-local: " . $_POST["ipv6-ll"] . "\n";
    $message .= "\n";

    switch ($tunneltype) {
        case "wireguard":
            $required_arguments = [
                "wg-pubkey",
                "wg-psk",
                "wg-endpoint",
                "wg-keepalive"
            ];
            check_arguments($required_arguments);

            // $message .= "WireGuard\n=========\n";
            $message .= "WireGuard public key: " . $_POST["wg-pubkey"] . "\n";
            $message .= "WireGuard PSK: " . $_POST["wg-psk"] . "\n";
            $message .= "Keepalive: " . $_POST["wg-keepalive"] . "\n";
            $message .= "Endpoint: " . $_POST["wg-endpoint"] . "\n";

            break;
        case "fastd":
            $required_arguments = [
                "fastd-mode",
                "fastd-method",
                "fastd-pubkey",
                "fastd-endpoint"
            ];
            check_arguments($required_arguments);

            // $message .= "fastd\n=====\n";
            $message .= "Mode: ";
            switch ($_POST["fastd-mode"]) {
                case "tun":
                    $message .= "TUN";
                    break;
                case "tap":
                    $message .= "TAP";
                    break;
                default:
                    throw new FormException("Wrong fastd mode");
                    break;
            }
            $message .= "\n";
            $message .= "Method: " . $_POST["fastd-method"] . "\n";
            $message .= "Connection over: IPv6\n";
            $message .= "Public key: " . $_POST["fastd-pubkey"] . "\n";
            $message .= "Endpoint: " . $_POST["fastd-endpoint"] . "\n";

            break;
        case "openvpn":
            $required_arguments = [
                "openvpn-mode",
                "openvpn-proto",
                "openvpn-cipher",
                "openvpn-endpoint",
                "openvpn-port",
                "openvpn-keepalive",
                "openvpn-keepalive-to"
            ];
            check_arguments($required_arguments);

            // $message .= "OpenVPN\n=======\n";
            $message .= "Mode: P2P\n";
            $message .= "Protocol: UDP6\n";
            $message .= "Cipher: AES-256-CBC\n";
            $message .= "Endpoint: " . $_POST["openvpn-endpoint"] . "\n";
            $message .= "Port: " . $_POST["openvpn-port"] . "\n";
            $message .= "Keepalive: " . $_POST["openvpn-keepalive"] . "\n";
            $message .= "Keepalive timeout: " . $_POST["openvpn-keepalive-to"] . "\n";

            break;
        case "gre":
            $required_arguments = [
                "gre-endpoint",
                "gre-fou",
                "gre-fou-port"
            ];
            check_arguments($required_arguments);

            // $message .= "GRE\n===\n";
            $message .= "Endpoint: " . $_POST["gre-endpoint"] . "\n";
            $message .= "TTL: 255\n";
            $message .= "FOU: " . $_POST["gre-fou"] . "\n";
            $message .= "FOU port: " . $_POST["gre-fou-port"] . "\n";

            break;
        case "vxlan":
            $required_arguments = [
                "vxlan-endpoint",
                "vxlan-port",
                "vxlan-vni"
            ];
            check_arguments($required_arguments);

            // $message .= "VXLAN\n=====\n";
            $message .= "Endpoint: " . $_POST["vxlan-endpoint"] . "\n";
            $message .= "Port: " . $_POST["vxlan-port"] . "\n";
            $message .= "VNI: " . $_POST["vxlan-vni"] . "\n";

            break;
    }

    $message .= "\n";
    $message .= "Additional comments:\n";
    $message .= $_POST["comments"];
    $message .= "\n\n";

    $message .= "Greetings\n" . ucwords($entity);

    $mail = new PHPMailer;

    $mail->SMTPDebug = 0;

    $mail->isSMTP();

    $mail->Host = 'mxe87b.netcup.net';
    $mail->Username = 'crxn-peering-request-form@byeob.de';
    $mail->Password = $passwd;
    $mail->Port = 587;

    $mail->SMTPAuth = true;
    $mail->SMTPSecure = 'tls';

    $mail->From = 'crxn-peering-request-form@byeob.de';
    $mail->FromName = 'CRXN peering request form';
    $mail->addAddress('crxn@mk16.de');
    $mail->addReplyTo($replay, $entity);


    $mail->WordWrap = 50;
    $mail->isHTML(false);

    $mail->Subject = 'CRXN peering request';
    $mail->Body    = $message;

    if(!$mail->send()) {
        echo 'The message could not be sent. Try again or send your request to crxn@mk16.de.<br>';
        echo 'Error: ' . $mail->ErrorInfo;
    } else {
        echo "Thank you for your inquiry! Your message has been sent.";
    }

} catch (phpmailerException $e) {
    echo "The message could not be sent. Try again or send your request to crxn@mk16.de.<br>";
    echo "Error: " . $e->errorMessage();
} catch (FormException $e) {
    echo "The message could not be sent. Try again or send your request to crxn@mk16.de.<br>";
    echo "Error: " . $e->errorMessage();
} catch (Exception $e) {
    echo "The message could not be sent. Try again or send your request to crxn@mk16.de.<br>";
    echo "Error: " . $e->getMessage();}

?>
        </main>
    </body>
</html>